package com.companyName.sample.rest.restful.sys;

import com.companyName.sample.api.service.IFdsService;
import com.companyName.sample.api.service.sys.IUserInfoService;
import com.companyName.sample.bean.dto.sys.UserInfoDto;
import com.companyName.sample.common.bean.ResponseData;
import com.companyName.sample.common.constant.ResponseMsg;
import com.companyName.sample.common.exception.BusinessException;
import com.companyName.sample.rest.restful.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@RestController
public class UserInfoController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(UserInfoController.class);
    @Autowired
    private IUserInfoService userInfoServiceImpl;
//    @Autowired
    private IFdsService fdsService;


    /**
     * 查询 ID
     */
    @RequestMapping(value = "/api/getuser/{userid}", method = RequestMethod.GET)
    @ExceptionHandler({NullPointerException.class})
    public String getUserInfo(@PathVariable(value = "userid") String userID) {
        ResponseData<UserInfoDto> responseData = null;
        try {
            UserInfoDto userInfoDto = new UserInfoDto();
//            userInfoDto = null;
            userInfoDto.setId(new Integer(userID));

            responseData = userInfoServiceImpl.getUserInfo(new Integer(userID));
        } catch (BusinessException e) {
            responseData = (ResponseData<UserInfoDto>) wrapExceptionResponseData(responseData, e.getMessage());
            log.error(e.getMessage());
            throw new NullPointerException("a");
        } catch (Exception e) {
            responseData = (ResponseData<UserInfoDto>) wrapExceptionResponseData(responseData, ResponseMsg.EXCEPTION.getMsg());
            log.error(e.getMessage());
            throw new NullPointerException("a");
        }
        return responseData.toString();
    }

    /**
     * 查询 List
     */
    @RequestMapping(value = "/api/getuserlist", method = RequestMethod.GET)
    public String getUserInfoList(@RequestParam(value = "username") String userName,
                                  @RequestParam(value = "page_id", defaultValue = "0") Integer pageId,
                                  @RequestParam(value = "page_size", defaultValue = "20") Integer pageSize) {
        ModelAndView model = new ModelAndView();
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUsername(userName);
        ResponseData responseData = userInfoServiceImpl.getUserInfoList(userInfoDto, pageId, pageSize);
        return responseData.toString();
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/api/putuser", method = RequestMethod.PUT)
    @ResponseBody
    public String putUserInfo(HttpServletRequest httpRequest,
                              @RequestParam(value = "name", required = false) String userName,
                              @RequestParam(value = "passwd", required = false) String passWd) {
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUsername(userName);
        userInfoDto.setPassword(passWd);
        ResponseData responseData = userInfoServiceImpl.putUserInfo(userInfoDto);
        return responseData.toString();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/api/postuser", method = RequestMethod.POST)
    @ResponseBody
    public String postUserInfo(HttpServletRequest httpRequest,
                               @RequestParam(value = "id", required = false) String userID,
                               @RequestParam(value = "name", required = false) String userName,
                               @RequestParam(value = "passwd", required = false) String passWd) {
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setId(new Integer(userID));
        userInfoDto.setUsername(userName);
        userInfoDto.setPassword(passWd);
        ResponseData responseData = userInfoServiceImpl.postUserInfo(userInfoDto);
        return responseData.toString();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/api/deleteuser", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteUserInfo(HttpServletRequest httpRequest,
                                 @RequestParam(value = "id", required = false) String userID) {
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setId(new Integer(userID));
        ResponseData responseData = userInfoServiceImpl.deleteUserInfo(userInfoDto);
        return responseData.toString();
    }

    /*
     * 增加，测试事务
     */
    @RequestMapping(value = "/api/putuserT", method = RequestMethod.PUT)
    @ResponseBody
    public String putUserInfoT(HttpServletRequest httpRequest,
                               @RequestParam(value = "name", required = false) String userName,
                               @RequestParam(value = "passwd", required = false) String passWd) {
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUsername(userName);
        userInfoDto.setPassword(passWd);
        ResponseData responseData;
        if ("T".equals(passWd)) {
            responseData = userInfoServiceImpl.putUserInfoT(userInfoDto);
        } else {
            responseData = userInfoServiceImpl.insertUserInfoT(userInfoDto);
        }
        return responseData.toString();
    }

    /**
     * 上传DFS的例子
     */
   /* public String uploadDFSFile(MultipartFile mediaUploadFile, MultipartFile thumbnailUploadFile) throws IOException {
        if (null == mediaUploadFile || null == mediaUploadFile) {
            log.warn("mediaUploadFile or mediaInfo is required");
            return (new ResponseData<String>(ResponseMsg.OBJECT_NOT_AVAILABLE.getCode(), "thumbnail object is not available")).toString();
        }

        int fdsType = 1;//上传类型  CommConstants.MediaType 视频 = 1; 图片 = 2; ALL = Video | Picture;
        String bucketName = "UserInfoBucket";
        String objectName = UUID.randomUUID().toString();
        String mediaObject = FDSUtil.getMediaObject(fdsType, objectName);
        String thumbnailObject = FDSUtil.getThumbObject(fdsType, objectName);
        String mediaUrl = fdsService.getDownloadUrl(bucketName, mediaObject);
        String thumbnailUrl = fdsService.getDownloadUrl(bucketName, thumbnailObject);

        fdsService.putObject(bucketName, mediaObject, mediaUploadFile.getInputStream(), "application/octet-stream");
        if (!fdsService.setObjectVisible(bucketName, mediaObject)) {
            log.warn("media url verify failed: objectName = {}", objectName);
            return (new ResponseData<String>(ResponseMsg.OBJECT_NOT_AVAILABLE.getCode(), "media object is not available")).toString();
        }

        if (thumbnailUploadFile != null && !thumbnailUploadFile.getOriginalFilename().equals("")) {
            fdsService.putObject(bucketName, thumbnailObject, thumbnailUploadFile.getInputStream());
            if (!fdsService.setObjectVisible(bucketName, thumbnailObject)) {
                log.warn("thumbnail url verify failed: objectName = {}", objectName);
                return (new ResponseData<String>(ResponseMsg.OBJECT_NOT_AVAILABLE.getCode(), "thumbnail object is not available")).toString();
            }
        } else {
            if (fdsType == (short) 2) {
                thumbnailUrl = mediaUrl + "?thumb=1&w=350&h=350";
            } else {
                thumbnailUrl = mediaUrl;
            }

        }

        //TODO   mediaUrl   thumbnailUrl to DB


        return (new ResponseData<String>(ResponseMsg.SUCCESS.getCode(), "Operation Succeed!")).toString();
    }*/
}
